﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;

namespace PPTWCF
{
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class TestInfoService
    {
        // To use HTTP GET, add [WebGet] attribute. (Default ResponseFormat is WebMessageFormat.Json)
        // To create an operation that returns XML,
        //     add [WebGet(ResponseFormat=WebMessageFormat.Xml)],
        //     and include the following line in the operation body:
        //         WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
        [OperationContract]
        public void startTest()
        {
            System.Threading.Thread t = new System.Threading.Thread(InitTest);
            t.Start();
            //TestThread();
        }

        public void InitTest()
        {
            System.Threading.Thread t1 = new System.Threading.Thread(TestThread);
            System.Threading.Thread t2 = new System.Threading.Thread(TestThread);
            System.Threading.Thread t3 = new System.Threading.Thread(TestThread);
            System.Threading.Thread t4 = new System.Threading.Thread(TestThread);

            t1.Start();
            System.Threading.Thread.Sleep(9000);
            t2.Start();
            System.Threading.Thread.Sleep(23000);
            t3.Start();
            System.Threading.Thread.Sleep(19000);
            t4.Start();
        }

        private List<hh> getList()
        {
            List<hh> newList = new List<hh>();
           

            newList.Add(new hh(47.47723F, 19.04717F)); //Moricz
            newList.Add(new hh(47.4766804F, 19.0509796F));

            newList.Add(new hh(47.47583F, 19.05409F)); //Budafoki ut

            newList.Add(new hh(47.4761147F, 19.05761F));

            newList.Add(new hh(47.47694F, 19.05961F)); //Petofi hid

            newList.Add(new hh(47.4783772F, 19.0623736F));

            newList.Add(new hh(47.4794939F, 19.0645838F));

            newList.Add(new hh(47.48035F, 19.06702F)); //Boraros

            newList.Add(new hh(47.4813211F, 19.0678024F));


            newList.Add(new hh(47.482118F, 19.068467F)); //Mester utva


            newList.Add(new hh(47.4825393F, 19.0687895F));
            newList.Add(new hh(47.4839314F, 19.0691972F));

            newList.Add(new hh(47.48579F, 19.07015F));//Corvin negyed

            newList.Add(new hh(47.4873535F, 19.0703559F));

            newList.Add(new hh(47.4889485F, 19.0706134F));

            newList.Add(new hh(47.48931F, 19.07139F)); //Harminckettesek tere

            newList.Add(new hh(47.4907029F, 19.0708709F));
            newList.Add(new hh(47.4919498F, 19.0708923F));

            newList.Add(new hh(47.49309F, 19.07193F)); //Rakoczi ter 

            newList.Add(new hh(47.4940666F, 19.0709352F));
            newList.Add(new hh(47.4956034F, 19.0706778F));


            newList.Add(new hh(47.4963F, 19.06961F)); //Blaha

            newList.Add(new hh(47.4983434F, 19.0700984F));
            newList.Add(new hh(47.4993437F, 19.0695405F));

            newList.Add(new hh(47.49977F, 19.06753F)); //Wesselenyi utca

            newList.Add(new hh(47.5013152F, 19.0678239F));
            newList.Add(new hh(47.5024314F, 19.0667295F));

            newList.Add(new hh(47.50234F, 19.06343F)); //kiraly utca

            newList.Add(new hh(47.5038955F, 19.0650558F));
            newList.Add(new hh(47.5046348F, 19.0642405F));


            newList.Add(new hh(47.50552F, 10.06369F)); //Oktogon

            newList.Add(new hh(47.5064322F, 19.061687F));
            newList.Add(new hh(47.5079251F, 19.0595627F));
            newList.Add(new hh(47.5091426F, 19.0577602F));

            newList.Add(new hh(47.50994F, 19.05664F)); //Nyugati

            newList.Add(new hh(47.5112007F, 19.0536189F));
            newList.Add(new hh(47.5121138F, 19.0511513F));
            newList.Add(new hh(47.512824F, 19.0490913F));

            newList.Add(new hh(47.51338F, 19.04683F)); //Jaszai Mari ter

            newList.Add(new hh(47.5139979F, 19.0455723F));
            newList.Add(new hh(47.514824F, 19.0432549F));
            newList.Add(new hh(47.514708F, 19.0404224F));

            newList.Add(new hh(47.51537F, 19.03951F)); //Margit hid, budai hidfo

            newList.Add(new hh(47.5146211F, 19.0367746F));
            newList.Add(new hh(47.5130993F, 19.0348649F));

            newList.Add(new hh(47.51204F, 19.03303F)); //Mechwart liget

            newList.Add(new hh(47.510679F, 19.0310454F));
            newList.Add(new hh(47.5097224F, 19.0288138F));

            newList.Add(new hh(47.50785F, 19.02678F)); //Szena ter

            newList.Add(new hh(47.5078236F, 19.0262389F));

            newList.Add(new hh(47.50694F, 19.0248F)); //Moszkva ter

            return newList;

        }

        public void TestThread()
        {
            InfoService c = new InfoService();
            Random r = new Random();
            int wait;

            string id = c.GetID(41.47723F, 16.04717F);
            
            List<hh> poss = this.getList();

            if (r.Next(2) == 1)
            {
                List<hh> newList = poss;

                for (int i = poss.Count; i > 0; i--)
                {
                    newList.Add(poss[i-1]);
                }

                poss = newList;
            }

            foreach (var item in poss)
            {
                c.InsertNewPosition(6, "V" ,  id, item.Lat, item.Lon);
                wait = r.Next(4000);
                
                System.Threading.Thread.Sleep(wait + 2000);
            }
        }

        class hh
        {
            public float Lat { get; set; }
            public float Lon { get; set; }

            public hh(float lat, float lon)
            {
                this.Lat = lat;
                this.Lon = lon;
            }

        }

        // Add more operations here and mark them with [OperationContract]
    }
}
